class FoldersController < ApplicationController
	before_filter :find_model_instance, :only => [:show, :edit, :update, :destroy]

  after_filter :update_cached_folders, :only => [:create, :update, :destroy, :sort_folders]

	after_filter :update_user_groups, :only => [:create, :update]

  # GET /folders
  # GET /folders.xml
  def index
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => Easel::Config.top_level_folders }
    end
  end

  # GET /folders/1
  # GET /folders/1.xml
  def show
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @folder }
    end
  end

  # GET /folders/new
  # GET /folders/new.xml
  def new
    @folder = Folder.new
    @user_groups_for_user = Lockdown::System.user_groups_assignable_for_user(current_user)

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @folder }
    end
  end

  # GET /folders/1/edit
  def edit
    @user_groups_for_user = Lockdown::System.user_groups_assignable_for_user(current_user)
  end

  # POST /folders
  # POST /folders.xml
  def create
		@folder = Folder.new(params[:folder])
    respond_to do |format|
      if @folder.save
        flash[:notice] = 'Folder was successfully created.'
        format.html { redirect_to(@folder) }
        format.xml  { render :xml => @folder, :status => :created, :location => @folder }
      else
				@user_groups_for_user = Lockdown::System.user_groups_assignable_for_user(current_user)
        format.html { render :action => "new" }
        format.xml  { render :xml => @folder.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /folders/1
  # PUT /folders/1.xml
  def update
    respond_to do |format|
      if @folder.update_attributes(params[:folder])
        flash[:notice] = 'Folder was successfully updated.'
        format.html { redirect_to(@folder) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @folder.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /folders/1
  # DELETE /folders/1.xml
  def destroy
    @folder.destroy

    respond_to do |format|
      format.html { redirect_to(folders_url) }
      format.xml  { head :ok }
    end
  end

  def sort_folders
    if params[:folder]
			params[:folder].each_with_index do |id, i|
        folder = Folder.find(id.to_i)
        folder.position = i + 1
        folder.save
      end
    end
    render :nothing => true 
  end

  def sort_contents
    if params[:content]
			params[:content].each_with_index do |id, i|
        content = Content.find(id.to_i)
        content.position = i + 1
        content.save
      end
    end
    render :nothing => true 
  end

	private

	def find_model_instance
		@folder = Folder.find_with_content(params[:id])
	end

  def update_cached_folders
    Easel::Config.build_top_level_folders
  end

  def update_user_groups 
    #formatted for readability
		new_user_group_ids = params.collect do |p|
      if p[0] =~ /^grp_/
        p[0].split("_")[1].to_i 
      end
    end.compact

    @folder.update_user_groups(new_user_group_ids)
    @folder.update_qualified_names
  end
end #end folders controller
